<template>
	<main-content
		:tableHeight.sync="tableHeight"
		:otherDependenComponents="otherDependenComponents"
	>
		<template slot="query">
			<el-form
				ref="form"
				:inline="true"
				:model="queryData"
				class="flex-warp-container"
			>
				<el-form-item
					:label="`${$t('common.belong_merchant')}:`"
					class="flex-row-container"
					label-width="77px"
				>
					<merchant-select
						v-model="queryData.merchantId"
						:merchantInfo.sync="merchantObj"
					></merchant-select>
				</el-form-item>
				<el-form-item class="flex-row-container">
					<el-select
						v-model="timeFlag"
						:placeholder="$t('common.please_choose')"
						style="width:140px;"
					>
						<el-option
							:label="
								$t(
									'funds.proxy_member_funds_record.arrive_time'
								)
							"
							value="1"
						></el-option>
						<el-option
							:label="$t('funds.apply_time')"
							value="0"
						></el-option>
					</el-select>

					<el-date-picker
						v-model="searchTime"
						size="medium"
						:picker-options="pickerShortcut2"
						format="yyyy-MM-dd HH:mm:ss"
						type="datetimerange"
						range-separator="-"
						:start-placeholder="$t('common.start_date')"
						:end-placeholder="$t('common.end_date')"
						align="right"
						:default-time="defaultTime"
						:clearable="false"
						class="time-options-value"
					></el-date-picker>
				</el-form-item>
				<el-form-item
					:label="
						`${$t('funds.proxy_member_funds_record.order_status')}:`
					"
					class="flex-row-container"
					label-width="77px"
				>
					<el-select
						v-model="queryData.orderStatus"
						clearable
						:placeholder="$t('common.select_all')"
						:popper-append-to-body="false"
					>
						<el-option
							v-for="item in patchAdjustStatus"
							:key="item.code"
							:label="item.description"
							:value="item.code"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item
					:label="`${$t('funds.proxy_name')}:`"
					class="flex-row-container"
					label-width="77px"
				>
					<el-input
						v-model="queryData.userName"
						clearable
						size="medium"
						:placeholder="$t('common.please_enter')"
						:disabled="loading"
						v-symbols
						oninput="value=value.replace(/[\u4E00-\u9FA5]/g ,'')"
						@keyup.enter.native="enterSearch"
					></el-input>
				</el-form-item>
				<el-form-item
					:label="`${$t('funds.order_no')}:`"
					class="flex-row-container"
					label-width="61px"
				>
					<el-input
						v-model="queryData.eventId"
						clearable
						size="medium"
						:placeholder="$t('common.please_enter')"
						:disabled="loading"
						v-symbols
						oninput="value=value.replace(/[\u4E00-\u9FA5]/g ,'')"
						@keyup.enter.native="enterSearch"
					></el-input>
				</el-form-item>
				<el-form-item
					:label="
						`${$t('funds.proxy_member_funds_record.wallet_type')}:`
					"
					class="flex-row-container"
					label-width="77px"
				>
					<el-select
						v-model="queryData.walletType"
						clearable
						:placeholder="$t('common.select_all')"
						:popper-append-to-body="false"
					>
						<el-option
							v-for="item in proxyWalletType"
							:key="item.code"
							:label="item.description"
							:value="item.code"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item
					:label="
						`${$t('funds.proxy_member_funds_record.adjust_type')}:`
					"
					class="flex-row-container"
					label-width="77px"
				>
					<el-select
						v-model="queryData.adjustType"
						clearable
						:placeholder="$t('common.select_all')"
						:popper-append-to-body="false"
						@change="selectAjustType()"
					>
						<el-option
							v-for="item in proxyPatchAddAdjustType"
							:key="item.code"
							:label="item.description"
							:value="item.code"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item
					:label="
						`${$t(
							'funds.proxy_member_funds_record.adjust_amount'
						)}:`
					"
					class="flex-row-container"
					label-width="77px"
				>
					<el-input
						v-model="queryData.adjustAmountMin"
						size="medium"
						clearable
						:placeholder="$t('common.minimum')"
						style="width: 130px"
						maxLength="15"
						v-input="{ name: 'onlyTwoNum' }"
						name="adjustAmountMin"
						@blur="
							handleAmount(
								queryData,
								1,
								'adjustAmountMin',
								'adjustAmountMax',
								$t(
									'funds.proxy_member_funds_record.form_search_tip5'
								)
							)
						"
					></el-input>
					-
					<el-input
						v-model="queryData.adjustAmountMax"
						size="medium"
						clearable
						:placeholder="$t('common.maximum')"
						style="width: 130px"
						maxLength="15"
						v-input="{ name: 'onlyTwoNum' }"
						name="adjustAmountMax"
						@blur="
							handleAmount(
								queryData,
								2,
								'adjustAmountMin',
								'adjustAmountMax',
								$t(
									'funds.proxy_member_funds_record.form_search_tip6'
								)
							)
						"
					></el-input>
				</el-form-item>
				<el-form-item style="margin-left: 8px">
					<el-button
						type="primary"
						icon="el-icon-search"
						size="medium"
						@click="search"
					>
						{{ $t('common.search') }}
					</el-button>
					<el-button
						icon="el-icon-refresh-left"
						:disabled="loading"
						size="medium"
						@click="reset"
					>
						{{ $t('common.reset') }}
					</el-button>
					<el-button
						v-if="hasPermission('405111')"
						icon="el-icon-download"
						type="warning"
						:disabled="loading"
						size="medium"
						:loading="exportBtnLoading"
						@click="handleExportExcel"
					>
						{{ $t('common.export') }}
					</el-button>
					<!-- <el-button
							type="success"
							icon="el-icon-sort"
							:disabled="loading"
							size="medium"
							@click="openColSettings"
						>
							{{ $t('common.row_set') }}
						</el-button> -->
				</el-form-item>
			</el-form>
		</template>
		<template slot="content">
			<el-table
				ref="tables"
				v-loading="loading"
				border
				size="mini"
				show-summary
				:data="tableData"
				style="width: 100%"
				class="small-size-table"
				:header-cell-style="getRowClass"
				:max-height="tableHeight"
				element-loading-spinner="el-icon-loading"
				highlight-current-row
				:summary-method="
					(val) => handleRow(val, subSummary, totalSummary)
				"
			>
				<el-table-column
					prop="eventId"
					align="center"
					:label="$t('funds.order_no')"
					min-width="260"
					fixed
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<Copy :title="scope.row.eventId" :copy="copy">
							{{ scope.row.eventId }}
						</Copy>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData[$t('funds.proxy_name')]"
					prop="userName"
					align="center"
					:label="$t('funds.proxy_name')"
					min-width="130"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<Copy :title="scope.row.userName" :copy="copy">
							{{ scope.row.userName }}
						</Copy>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData[$t('common.belong_merchant')]"
					prop="merchantName"
					align="center"
					:label="$t('common.belong_merchant')"
					min-width="160"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ merchantFilter(scope.row.merchantId) }}
					</template>
				</el-table-column>
				<el-table-column
					v-if="
						tableColumnData[
							$t('funds.proxy_member_funds_record.order_status')
						]
					"
					prop="orderStatus"
					align="center"
					:label="$t('funds.proxy_member_funds_record.order_status')"
					min-width="100"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{
							typeFilter(
								scope.row.orderStatus,
								'patchAdjustStatus'
							)
						}}
					</template>
				</el-table-column>
				<el-table-column
					v-if="
						tableColumnData[
							$t('funds.proxy_member_funds_record.wallet_type')
						]
					"
					prop="walletType"
					align="center"
					:label="$t('funds.proxy_member_funds_record.wallet_type')"
					min-width="90"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span v-if="scope.row.walletType === 17">
							{{ $t('dict.proxy_wallet_type.0.description') }}
						</span>
						<span v-else-if="scope.row.walletType === 18">
							{{ $t('dict.proxy_wallet_type.1.description') }}
						</span>
						<span v-else-if="scope.row.walletType === 21">
							{{ $t('dict.proxy_wallet_type.2.description') }}
						</span>
						<span v-else>
							-
						</span>
					</template>
				</el-table-column>
				<el-table-column
					v-if="
						tableColumnData[
							$t('funds.proxy_member_funds_record.adjust_type')
						]
					"
					prop="adjustType"
					align="center"
					:label="$t('funds.proxy_member_funds_record.adjust_type')"
					min-width="160"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{
							typeFilter(
								scope.row.adjustType,
								'proxyPatchAddAdjustType'
							)
						}}
					</template>
				</el-table-column>
				<el-table-column
					v-if="
						tableColumnData[
							$t('funds.proxy_member_funds_record.adjust_amount')
						]
					"
					prop="adjustAmount"
					align="center"
					min-width="180"
					:label="$t('funds.proxy_member_funds_record.adjust_amount')"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ handleCurrency(scope.row.currency) }}
						{{
							handleNumber(
								scope.row.currency,
								scope.row.adjustAmount
							)
						}}
					</template>
				</el-table-column>
				<el-table-column
					v-if="
						tableColumnData[
							$t('funds.proxy_member_funds_record.annex_file')
						]
					"
					prop="imageAddress"
					align="center"
					min-width="180"
					:label="$t('funds.proxy_member_funds_record.annex_file')"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<el-link
							v-if="scope.row.imageAddress"
							class="lightBlueColor"
							type="primary"
							@click="dialogData(scope.row)"
						>
							{{ $t('funds.proxy_member_funds_record.checks') }}
						</el-link>
						<span v-else>-</span>
					</template>
				</el-table-column>
				<el-table-column
					v-if="
						tableColumnData[
							$t('funds.proxy_member_funds_record.proposer')
						]
					"
					prop="operator"
					align="center"
					:label="$t('funds.proxy_member_funds_record.proposer')"
					min-width="150"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operator || '-' }}
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData[$t('funds.apply_time')]"
					prop="createdAt"
					align="center"
					:label="$t('funds.apply_time')"
					min-width="160px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.createdAt || '-' }}
					</template>
				</el-table-column>
				<el-table-column
					v-if="
						tableColumnData[
							$t('funds.proxy_member_funds_record.arrive_time')
						]
					"
					prop="audit2Time"
					align="center"
					:label="$t('funds.proxy_member_funds_record.arrive_time')"
					min-width="160px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span>
							{{ scope.row.audit2Time || '-' }}
						</span>
					</template>
				</el-table-column>
				<el-table-column
					v-if="tableColumnData[$t('common.remark')]"
					align="center"
					:label="$t('common.remark')"
					min-width="220"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.remark || '-' }}
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<el-pagination
				v-show="!!total"
				:current-page.sync="pageNum"
				class="pageValue"
				background
				layout="total, sizes,prev, pager, next, jumper"
				:page-size="pageSize"
				:page-sizes="pageSizes"
				:total="total"
				:pager-count="9"
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
			></el-pagination>
		</template>
		<image-viewer
			v-if="imageViewerVisible"
			:visible.sync="imageViewerVisible"
			:url-list="imageViewerSrc"
		/>
		<el-dialog
			:title="$t('common.row_set')"
			center
			:visible.sync="colSettings.visible"
			width="475px"
			class="setting-div"
		>
			<div>
				<el-link type="primary" class="mb15" @click="clickDel">
					复原缺省
				</el-link>
			</div>
			<div
				v-for="(value, name1) in activityListField"
				:key="name1"
				class="setting-div"
			>
				<el-checkbox v-model="dialogColumnData[name1]">
					{{ name1 }}
				</el-checkbox>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="colSettings.visible = false">
					{{ $t('common.cancel') }}
				</el-button>
				<el-button type="primary" @click="handleOrderList">
					{{ $t('common.submit') }}
				</el-button>
			</div>
		</el-dialog>
	</main-content>
</template>

<script>
import dayjs from 'dayjs'
import list from '@/mixins/list'
import { routerNames } from '@/utils/consts'
import summaryList from './components/summaryList'
import MainContent from '@/components/MainContent/index.vue'
const end = () => {
	return dayjs().endOf('day')
}
const start = () => {
	return dayjs().startOf('day')
}
export default {
	name: routerNames.proxyTopupRecord,
	components: {
		MainContent
	},
	mixins: [list, summaryList],
	data() {
		return {
			tableHeight: 480,
			otherDependenComponents: [],
			queryData: {
				adjustAmountMin: undefined,
				adjustAmountMax: undefined
			},
			timeFlag: '1',
			searchTime: [start(), end()],
			tableData: [],
			subSummary: {},
			totalSummary: {},
			imageViewerVisible: false,
			imageViewerSrc: [],
			exportBtnLoading: false,
			merchantObj: '',
			proxyPatchAddAdjustType: [],
			colSettings: {
				visible: false,
				dialogColumnData: {}
			},
			activityListField: {
				// 订单号: true,
				// 代理账号: true,
				// 所属商户: true,
				// 订单状态: true,
				// 钱包类型: true,
				// 调整类型: true,
				// 调整金额: true,
				// 附件预览: true,
				// 申请人: true,
				// 申请时间: true,
				// 到账时间: true,
				// 备注: true
				[this.$t('funds.proxy_name')]: true,
				[this.$t('common.belong_merchant')]: true,
				[this.$t('funds.proxy_member_funds_record.order_status')]: true,
				[this.$t('funds.proxy_member_funds_record.wallet_type')]: true,
				[this.$t('funds.proxy_member_funds_record.adjust_type')]: true,
				[this.$t(
					'funds.proxy_member_funds_record.adjust_amount'
				)]: true,
				[this.$t('funds.proxy_member_funds_record.annex_file')]: true,
				[this.$t('funds.proxy_member_funds_record.proposer')]: true,
				[this.$t('funds.apply_time')]: true,
				[this.$t('funds.proxy_member_funds_record.arrive_time')]: true,
				[this.$t('common.remark')]: true
			}
		}
	},
	computed: {
		accountType() {
			return this.globalDics.accountType || []
		},
		patchAdjustStatus() {
			return this.globalDics.patchAdjustStatus || []
		},
		proxyWalletType() {
			return this.globalDics.proxyWalletType || []
		}
	},
	watch: {
		// timeFlag: {
		// 	handler(newVal) {
		// 		this.tableData = []
		// 		this.total = 0
		// 	}
		// },
		'queryData.walletType': {
			handler(newVal) {
				if (newVal) {
					this.getProxyGetWalletAdjustType(newVal)
				}
			},
			immediate: true
		}
	},
	created() {
		this.initIndexDB('proxyTopupRecord', this.activityListField)
		this.getProxyGetWalletAdjustType(0)
	},
	methods: {
		selectAjustType() {
			this.$forceUpdate()
		},
		getProxyGetWalletAdjustType(val) {
			/**
			 *  钱包类型 - walletType
			 */
			this.proxyPatchAddAdjustType = []
			this.queryData.adjustType = undefined
			this.$api
				.getProxyGetWalletAdjustType({ walletType: val })
				.then((res) => {
					this.proxyPatchAddAdjustType = res?.data || []
				})
		},
		loadData() {
			this.loading = true
			const timeArr = this.searchTime || []
			const params = {
				...this.queryData,
				timeType: this.timeFlag,
				timeStart: dayjs(timeArr[0]).format('YYYY-MM-DD HH:mm:ss'),
				timeEnd: dayjs(timeArr[1]).format('YYYY-MM-DD HH:mm:ss'),
				...this.getParams()
			}
			this.pageCurrency = this.merchantObj?.currency || ''
			this.$api
				.getProxyArtificialAddRecordList(params)
				.then((res) => {
					const { record, totalRecord, summary } = res.data || {}
					this.tableData = record || []
					this.total = totalRecord || 0
					this.subSummary = summary || {}
					this.loading = false
				})
				.catch(() => {
					this.loading = false
				})
			this.$api
				.getProxyArtificialAddRecordSummary(params)
				.then((res) => {
					this.totalSummary = res.data || {}
					this.loading = false
				})
				.catch(() => {
					this.loading = false
				})
		},
		dialogData(val) {
			this.imageViewerSrc = val.imageAddress.split(',')
			this.imageViewerVisible = true
		},
		reset() {
			this.pageNum = 1
			this.queryData = {
				adjustAmountMin: undefined,
				adjustAmountMax: undefined
			}
			this.getProxyGetWalletAdjustType(0)
			this.timeFlag = '1'
			this.merchantObj = ''
			this.searchTime = [start(), end()]
			this.loadData()
		},
		reloadTime() {
			this.searchTime = [start(), end()]
		},
		handleExportExcel() {
			const timeArr = this.searchTime || []
			const params = {
				...this.queryData,
				timeType: this.timeFlag,
				timeStart: dayjs(timeArr[0]).format('YYYY-MM-DD HH:mm:ss'),
				timeEnd: dayjs(timeArr[1]).format('YYYY-MM-DD HH:mm:ss')
			}
			this.exportBtnLoading = true
			this.exportExcelPublic({
				api: 'getProxyArtificialAddRecordExport',
				params
			}).finally(() => {
				this.exportBtnLoading = false
			})
		},
		// 提交列设置数据
		handleOrderList() {
			this.colSettings.visible = false
			this.updateIndexDB()
		},
		// 打开列设置弹框
		openColSettings() {
			this.getIndexDB(this.activityListField)
			this.colSettings.visible = true
		},
		clickDel() {
			this.dialogColumnData = {}
			this.dialogColumnData = {
				// 订单号: true,
				// 代理账号: true,
				// 所属商户: true,
				// 订单状态: true,
				// 钱包类型: true,
				// 调整类型: true,
				// 调整金额: true,
				// 附件预览: true,
				// 申请人: true,
				// 申请时间: true,
				// 到账时间: true,
				// 备注: true
				[this.$t('funds.proxy_name')]: true,
				[this.$t('common.belong_merchant')]: true,
				[this.$t('funds.proxy_member_funds_record.order_status')]: true,
				[this.$t('funds.proxy_member_funds_record.wallet_type')]: true,
				[this.$t('funds.proxy_member_funds_record.adjust_type')]: true,
				[this.$t(
					'funds.proxy_member_funds_record.adjust_amount'
				)]: true,
				[this.$t('funds.proxy_member_funds_record.annex_file')]: true,
				[this.$t('funds.proxy_member_funds_record.proposer')]: true,
				[this.$t('funds.apply_time')]: true,
				[this.$t('funds.proxy_member_funds_record.arrive_time')]: true,
				[this.$t('common.remark')]: true
			}
		}
	}
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed-footer-wrapper .cell,
::v-deep .el-table__footer-wrapper .cell {
	height: 100%;
	padding: 0 !important;
	margin: 0 !important;
	> div {
		height: 100%;
	}
}
::v-deep .footer_count_row {
	min-height: 50px;
	height: 50%;
	display: flex;
	font-weight: 700;
	padding: 5px 0;
	align-items: center;
	flex-direction: column;
	justify-content: center;
}
::v-deep .footer_count_row_border {
	border-top: 1px solid #ebeef5;
}
</style>
